var TM_ServerList = new Class ({
	serverlistarray:[],
	serverlisttypearray:[],
	servertimestamp:0,
	clienttimestamp:0,
	difference:0,
	myJSON:null,

	initialize: function(){
                this.myJSON = new Request.JSON(
                {
                    url:homeBase+"inc/server_list.php?"+ AddUrlParam(),
                    method: 'post',
                    async: false,
                    data:JSON.encode({ "act":"getall_serverlisttype" }),
                    urlEncoded: false,
                    headers: {"Content-type": "application/json"}
                });
                this.myJSON.send();
                this.errcode=this.myJSON.response.json.errcode;
                this.servertimestamp=parseInt(this.myJSON.response.json.timestamp)*1000; 
                if(this.myJSON.response.json.response=="ERR")
                {
                	this.serverlistarray={};
                	this.serverlisttypearray={};
                }
                else
                {
                	this.serverlistarray=this.myJSON.response.json.result['serverlist'];
                	this.serverlisttypearray=this.myJSON.response.json.result['serverlisttype'];
                }
	},

	adjustLastCheckTimedifference:function(){
		if( this.errcode==0)
		{
			this.clienttimestamp=new Date().getTime();
			this.difference=this.clienttimestamp-this.servertimestamp;
			$each(this.serverlistarray,function(item,key){
				item.lastcheck=parseInt(item.lastcheck)*1000+this.difference;
			}.bind(this));
	       }
	},
	
	updateLastCheck:function(id,lastcheck){
	     for(var index=0;index<this.serverlistarray.length;index++) 
	     {
	     	if(this.serverlistarray[index].id==id)
	     	{
	     		 this.serverlistarray[index].lastcheck=lastcheck;
	     		 break;
	     	}
	     }
	},

	updateName:function(id,name,extra){
	     for(var index=0;index<this.serverlistarray.length;index++) 
	     {
	     	if(this.serverlistarray[index].id==id)
	     	{
	     		 this.serverlistarray[index].name=name;
                 var UriObj = parseUrl(name);
	     		 this.serverlistarray[index].hostinfo.host=UriObj.host.toLowerCase();
	     		 this.serverlistarray[index].hostinfo.port=UriObj.port;
	     		 this.serverlistarray[index].hostinfo.protocol=UriObj.protocol.toLowerCase();
                 this.serverlistarray[index].hostinfo.type=extra.servData[id].type;
                 this.serverlistarray[index].hostinfo.customFields=extra.servData[id].customFields;
	     		 break;
	     	}
	     }
	},
	
	getClientTimeStamp:function(){
		return this.clienttimestamp;
	},
		
	setServerList:function(newServerList){
		if($chk(newServerList) )
			this.serverlistarray=newServerList;
	},
	
	getServerList:function(){
		return this.serverlistarray;	
	},
	
	getServerListType:function(){
		return this.serverlisttypearray;	
	},
	
	isPasswordOnly:function(type){
		for(var i=0,l=this.serverlisttypearray.length;i<l;i++) 
	    {
			var item=this.serverlisttypearray[i];
			if(item.type===type)
			{
				if(item.passwordonly==="1")
					return true;
			}
	    }
		return false;
	},
	
	addOneServer:function(obj){
	     if($chk(obj))
		this.serverlistarray.push(obj);
	},
	
	deleteOneServer:function(index){
		if($chk(this.serverlistarray[index]))
			this.serverlistarray.splice(index,1);
	},
    getSeverAddress: function(id){
	     for(var index=0;index<this.serverlistarray.length;index++) 
	     {
	     	if(this.serverlistarray[index].id==id)
	     	{              
	     		 return this.serverlistarray[index].name;
	     	}
	     }
    }
 
});
 


